﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.Entities;
using DAL;
using Model.Account;

namespace Syncar.Web.FrontEnd.Controllers
{ 
    public class PublicationController : Controller
    {
        private SyncarContext db = new SyncarContext();
        
        private User GetLoggedUser()
        {
            //Load the user with his vehicles
            return db.FindUserByUserName(User.Identity.Name).Include(x => x.Publications).FirstOrDefault();
        }

        //
        // GET: /Publication/

        public ViewResult Index()
        {
            var user = GetLoggedUser();
            return View(user.Publications.ToList());
        }

        //
        // GET: /Publication/Details/5

        public ViewResult Details(int id)
        {
            Publication publication = db.Publications.Find(id);
            return View(publication);
        }

        //
        // GET: /Publication/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Publication/Create

        [HttpPost]
        public ActionResult Create(Publication publication)
        {
            var user = GetLoggedUser();
            if (ModelState.IsValid)
            {
                db.Publications.Add(publication);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(publication);
        }
        
        //
        // GET: /Publication/Edit/5
 
        public ActionResult Edit(int id)
        {
            Publication publication = db.Publications.Find(id);
            return View(publication);
        }

        //
        // POST: /Publication/Edit/5

        [HttpPost]
        public ActionResult Edit(Publication publication)
        {
            if (ModelState.IsValid)
            {
                db.Entry(publication).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(publication);
        }

        //
        // GET: /Publication/Delete/5
 
        public ActionResult Delete(int id)
        {
            Publication publication = db.Publications.Find(id);
            return View(publication);
        }

        //
        // POST: /Publication/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Publication publication = db.Publications.Find(id);
            db.Publications.Remove(publication);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}